'''An editing mode that supports python, for the text editor'''

import wx
import keyword

class PythonMode(object):
    def apply(self, widget):
        widget.SetLexer(wx.stc.STC_LEX_PYTHON)
        widget.SetUseTabs(0)
        widget.SetIndent(4)
        widget.SetBackSpaceUnIndents(1)
        widget.SetTabWidth(4)
        widget.SetTabIndents(True)
        widget.SetKeyWords(0, " ".join(keyword.kwlist))
        widget.SetViewWhiteSpace(False)
        widget.SetProperty("fold", "1")
        widget.SetProperty("tab.timmy.whinge.level", "1")
        widget.SetMargins(0,0)
        widget.SetMarginType(2, wx.stc.STC_MARGIN_SYMBOL)
        widget.SetMarginMask(2, wx.stc.STC_MASK_FOLDERS)
        widget.SetMarginSensitive(2, True)
        widget.SetMarginWidth(2, 12)
        widget.SetCaretForeground("#e22222")
        # note to self: these colors need to come from preferences...
        widget.MarkerDefine(wx.stc.STC_MARKNUM_FOLDEROPEN,    
                            wx.stc.STC_MARK_BOXMINUS, "white", "#404040")
        widget.MarkerDefine(wx.stc.STC_MARKNUM_FOLDER,        
                            wx.stc.STC_MARK_BOXPLUS,"white", "#404040")
        widget.MarkerDefine(wx.stc.STC_MARKNUM_FOLDERSUB,     
                            wx.stc.STC_MARK_VLINE, "white", "#404040")
        widget.MarkerDefine(wx.stc.STC_MARKNUM_FOLDERTAIL,    
                            wx.stc.STC_MARK_LCORNERCURVE, "white", "#404040")
        widget.MarkerDefine(wx.stc.STC_MARKNUM_FOLDEREND,     
                            wx.stc.STC_MARK_BOXPLUSCONNECTED, "white", "#404040")
        widget.MarkerDefine(wx.stc.STC_MARKNUM_FOLDEROPENMID, 
                            wx.stc.STC_MARK_BOXMINUSCONNECTED,"white", "#404040")
        widget.MarkerDefine(wx.stc.STC_MARKNUM_FOLDERMIDTAIL, 
                            wx.stc.STC_MARK_TCORNERCURVE, "white", "#404040")

        widget.SmartIndent = self.SmartIndent
        self.widget = widget

    def SmartIndent(self, event):
        print "python smart indent!", self, self.widget
        event.Skip()
        # this isn't very smart, but I (roughly) know how to solve 
        # the problem. 
        pos = self.widget.GetCurrentPos()
        line = self.widget.LineFromPosition(pos)
        print "line:", line
        if line == 0:
            event.Skip()
        else:
            # this isn't smart enough. For one, it should only indent
            # a line if the cursor is before the first non-whitespace
            # character on the line
            linestart = self.widget.PositionFromLine(line-1)
            # find first non-whitespace character
            pos = self.widget.FindText(linestart, self.widget.GetCurrentPos(),
                                       "[^ ]", wx.stc.STC_FIND_REGEXP)
            # Argh! stupid widget. This only works if there is a character
            # on the line, but I don't really want to add a character.
            # What I need to do instead is insert N spaces since I know
            # indentation is based on spaces rather than tabs.
            print "setting indentation for line %s to %s" % (line, (pos-linestart))
            self.widget.AddText(" ")
            self.widget.SetLineIndentation(line, (pos-linestart))
#            self.widget.AddText(" "*(pos-linestart))

